UICardDeck = UIBase.New(PanelNames.UICardDeck)

local defaultCharactorId = 101

function UICardDeck:OnCreate()
    self.btnBack = self:GetChild("Btn_Back")
    self.btnChangeName = self:GetChild("Loader_ChangeName01")
    self.btnUse = self:GetChild("Btn_UseCard")
    self.txtTitle = self:GetChild("Text_Title")
    --self.listDeck = self:GetList("list_deck")
    self.listDeckNew = self:GetList("List_Hero")
    self.listCard = self:GetList("List_Card")
    self.comSkill1 = self:GetChild("Com_SkillNew")
    self.comSkill2 = self:GetChild("Com_Skill2")
    self.iconCharactor = self:GetChild("Loader_LiHui2D")
    self.selectedList = {}
    self:AddEvent()
    self:InitView()
end

function UICardDeck:AddEvent()
    self:RegisterOnClick(self.btnBack,self.OnClickBack)
    self:RegisterOnClick(self.btnChangeName,self.OnClickChangeName)
    self:RegisterOnClick(self.btnUse,self.OnClickUse)
    --self:SetItemRenderer(self.listDeck,self.DeckItemRender,nil, true)
    self:SetItemRenderer(self.listCard,self.CardItemRender,nil, true)
    self:SetItemRenderer(self.listDeckNew,self.NewDeckItemRender,nil, true)
    self:RegisterEvent(UIEvent.DeckRename, self.Refresh)
    self:RegisterEvent(UIEvent.FreshCardDeck, self.Refresh)
end

function UICardDeck:InitView()
    self.curSelDeckIdx = 1
    self.curUseDeckIdx = decodeJsonStrToTable(GetPlayerPrefs("SelectedDeckIndex")) or 1
    self:Refresh()
end

function UICardDeck:Refresh()
    self:RefreshShowUseBtn()
    local deckName = GetPlayerPrefs("DeckName"..self.curSelDeckIdx)
    if deckName and deckName ~= "" then
        self.txtTitle.text = deckName
    else
        self.txtTitle.text = "卡组"..self.curSelDeckIdx
    end
    local cardList = decodeJsonStrToTable(GetPlayerPrefs("SelectedHeroList"..self.curSelDeckIdx))
    if cardList then
        self.selectedList = {}
        for k,v in pairs(cardList) do
            if DataTable.GetById("t_hero",v) then
                table.insert(self.selectedList,v)
            end
        end
    else
        self.selectedList = {}
    end
    self.listDeckNew.numItems = 3
    self.listCard.numItems = 8
    self:RefreshSkill()
end

function UICardDeck:RefreshSkill()
    local charactorId = decodeJsonStrToTable(GetPlayerPrefs("SelectedMainCharactor"..self.curSelDeckIdx)) --or defaultCharactorId
    local txtSkillName1 = self.comSkill1:GetChild("Text_SkillName")
    local txtSkillDesc1 = self.comSkill1:GetChild("Text_SkillDesc")
    txtSkillName1.text = ""
    txtSkillDesc1.text = ""

    local txtSkillName2 = self.comSkill2:GetChild("Text_SkillName")
    local txtSkillDesc2 = self.comSkill2:GetChild("Text_SkillDesc")
    txtSkillName2.text = ""
    txtSkillDesc2.text = ""

    if charactorId then
        self.iconCharactor.url = IconUtil.GetHeroBigIconUrl(charactorId) or ""
        --self.iconCharactor.scale = (Vector2(0.5, 0.5))
        local heroInfo = DataTable.GetById("t_hero",charactorId)
        if heroInfo then
            local skillId1 = heroInfo.f_SkillID
            if skillId1 then
                local skillInfo = DataTable.GetById("t_skill",skillId1)
                if skillInfo and skillInfo.f_SkillNameKey and skillInfo.f_SkillDescribeKey then
                    txtSkillName1.text = skillInfo.f_SkillNameKey
                    txtSkillDesc1.text = skillInfo.f_SkillDescribeKey
                end
            end
            local skillId2 = heroInfo.f_Characteristic
            if skillId2 and skillId2 ~= "" then
                self.comSkill2.visible = true
                local skillInfo = DataTable.GetById("t_skill",skillId2)
                if skillInfo and skillInfo.f_SkillNameKey and skillInfo.f_SkillDescribeKey then
                    txtSkillName2.text = skillInfo.f_SkillNameKey
                    txtSkillDesc2.text = skillInfo.f_SkillDescribeKey
                end
            else
                self.comSkill2.visible = false
            end
        end
    end
end

function UICardDeck:DeckItemRender(index, go)
    local ctrlIsSelected = go:GetController("ischoice")
    local ctrlIsUse = go:GetController("isUse")
    local txtName = go:GetChild("text_deckName")
    local deckName = GetPlayerPrefs("DeckName"..index)
    if deckName and deckName ~= "" then
        txtName.text = deckName
    else
        txtName.text = "卡组"..index
    end
    if index == self.curSelDeckIdx then
        ctrlIsSelected.selectedIndex = 1
    else
        ctrlIsSelected.selectedIndex = 0
    end
    if index == self.curUseDeckIdx then
        ctrlIsUse.selectedIndex = 1
    else
        ctrlIsUse.selectedIndex = 0
    end
    go.onClick:Clear()
    go.onClick:Add(function()
        self.curSelDeckIdx = index
        self:Refresh()
    end
    )
end

function UICardDeck:NewDeckItemRender(index, go)
    local ctrlIsSelected = go:GetController("select")
    local ctrlIsUse = go:GetController("type")
    local txtName = go:GetChild("Text_Name")
    --local ctrlIcon = go:GetChild("n9"):GetController("Sex")
    local imgIcon = go:GetChild("Btn_HeadIcom"):GetChild("Loader_Head")
    local deckName = GetPlayerPrefs("DeckName"..index)
    if deckName and deckName ~= "" then
        txtName.text = deckName
    else
        txtName.text = "卡组"..index
    end
    if index == self.curSelDeckIdx then
        ctrlIsSelected.selectedIndex = 1
    else
        ctrlIsSelected.selectedIndex = 0
    end
    if index == self.curUseDeckIdx then
        ctrlIsUse.selectedIndex = 0
    else
        ctrlIsUse.selectedIndex = 1
    end
    local charactorId = decodeJsonStrToTable(GetPlayerPrefs("SelectedMainCharactor"..index)) --or defaultCharactorId
    if charactorId and charactorId ~= "" then
        local cfg = DataTable.GetById("t_heroresources",charactorId)
        if cfg then
            imgIcon.url = IconUtil.GetCharactorDeckIconUrl(charactorId)
        else
            imgIcon.url = ""
        end
    end
    go.onClick:Clear()
    go.onClick:Add(function()
        self.curSelDeckIdx = index
        self:Refresh()
    end
    )
end

function UICardDeck:CardItemRender(index, go)
    local comCard = go:GetChild("Com_Card")
    local imgIcon = comCard:GetChild("Loader_Draw")
    local txtName = comCard:GetChild("Text_Name")
    local txtCost = comCard:GetChild("text_costNum")
    local ctrlEmpty = go:GetController("hasChoice")
    local data = self.selectedList[index]
    if data then
        ctrlEmpty.selectedIndex = 0
        local cfg = DataTable.GetById("t_hero",data)
        imgIcon.url = IconUtil.GetHeroCardIconUrl(data)
        txtCost.text = tostring(cfg.f_ConsumeEnergy)
        txtName.text = cfg.f_HeroName
    else
        ctrlEmpty.selectedIndex = 1
    end
    go.onClick:Clear()
    go.onClick:Add(function()
        showUI(PanelNames.UICardChoice, self.curSelDeckIdx, true)
    end)
end

function UICardDeck:OnClickChangeName()
    showUI(PanelNames.UIReNamePopup, self.curSelDeckIdx)
end

function UICardDeck:OnClickUse()
    if not (self.curUseDeckIdx == self.curSelDeckIdx) then
        if self.selectedList and #self.selectedList > 0 then
            self.curUseDeckIdx = self.curSelDeckIdx
            self:Refresh()
            SetPlayerPrefs("SelectedDeckIndex",getJsonStr(self.curSelDeckIdx))
        else
            logError("选择的卡牌数量不能为0")
            return
        end
    end
end

function UICardDeck:RefreshShowUseBtn()
    if self.curSelDeckIdx == self.curUseDeckIdx then
        self.btnUse.visible = false
    else
        self.btnUse.visible = true
    end
end

function UICardDeck:OnClickBack()
    FireEvent(UIEvent.FreshHeroSelect)
    closeUI(PanelNames.UICardDeck)
end

return UICardDeck